// TrackEditorView.cpp : implementation of the CTrackEditorView class
//

#include "stdafx.h"
#include "TrackEditor.h"

#include "TrackEditorDoc.h"
#include "TrackEditorView.h"
#include "Track.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CTrackEditorView

IMPLEMENT_DYNCREATE(CTrackEditorView, CScrollView)

BEGIN_MESSAGE_MAP(CTrackEditorView, CScrollView)
END_MESSAGE_MAP()

// CTrackEditorView construction/destruction

CTrackEditorView::CTrackEditorView()
{
	// TODO: add construction code here

}

CTrackEditorView::~CTrackEditorView()
{
}

BOOL CTrackEditorView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

void CTrackEditorView::OnInitialUpdate()
{
	SetScrollSizes(MM_TEXT, CSize(2000, 1000));
	SetScrollPos(SB_HORZ, 500, false);
	SetScrollPos(SB_VERT, 100, false);
}

// CTrackEditorView drawing
UINT GetResourceNameFromSymbol(char Symbol)
{
   switch (Symbol)
   {
		case CARACTER_SIMBOLO_RETA_INICIO:
			return IDB_RETA_INI;
			break;
		case CARACTER_SIMBOLO_RETA_NORMAL:
			return IDB_RETA;
			break;
		case CARACTER_SIMBOLO_CURVA_ESQUERDA:
			return IDB_CURVA_ESQ;
			break;
		case CARACTER_SIMBOLO_CURVA_DIREITA:
			return IDB_CURVA_DIR;
			break;
   }
   return  0;
}

CTrack* GetTrackFromSymbol(char Symbol)
{
   switch (Symbol)
   {
		case CARACTER_SIMBOLO_RETA_INICIO:
			return &trackRetaInicial;
			break;
		case CARACTER_SIMBOLO_RETA_NORMAL:
			return &trackReta;
			break;
		case CARACTER_SIMBOLO_CURVA_ESQUERDA:
			return &trackCurvaEsquerda;
			break;
		case CARACTER_SIMBOLO_CURVA_DIREITA:
			return &trackCurvaDireita;
			break;
   }
   return  0;
}

void CTrackEditorView::OnDraw(CDC* pDC)
{
	CTrackEditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	const char* docTracks = pDoc->GetTracks();

	float anguloAtual = 0;
	int xAtual = 1000, yAtual = 500;

	while (*docTracks != CARACTER_SIMBOLO_FINAL_CIRCUITO)
	{
	   CBitmap bmp;
	   CTrack* pTrack;

	   if (bmp.LoadBitmap(GetResourceNameFromSymbol(*docTracks)))
	   {
		  // Get the size of the bitmap
		  BITMAP bmpInfo;
		  bmp.GetBitmap(&bmpInfo);

		  // Create an in-memory DC compatible with the
		  // display DC we're using to paint
		  CDC dcMemory;
		  dcMemory.CreateCompatibleDC(pDC);

		  // Select the bitmap into the in-memory DC
		  CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);

		  // Find a centerpoint for the bitmap in the client area
		  //CRect rect;
		  //GetClientRect(&rect);
		  //int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2;
		  //int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;

		  // Copy the bits from the in-memory DC into the on-
		  // screen DC to actually do the painting. Use the centerpoint
		  // we computed for the target offset.
		  pDC->BitBlt(xAtual, yAtual, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 
			 0, 0, SRCCOPY);

		  dcMemory.SelectObject(pOldBitmap);
	   }
	   else
	   {
		  TRACE0("ERROR: Where's IDB_BITMAP1?\n");
	   }


		docTracks++;
	}
}


// CTrackEditorView diagnostics

#ifdef _DEBUG
void CTrackEditorView::AssertValid() const
{
	CView::AssertValid();
}

void CTrackEditorView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTrackEditorDoc* CTrackEditorView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTrackEditorDoc)));
	return (CTrackEditorDoc*)m_pDocument;
}
#endif //_DEBUG


// CTrackEditorView message handlers
